import os
import hashlib

def generateInode(bucketName, relativePath, filename, filesize):
    fileID = hashlib.md5(relativePath.encode(encoding='UTF-8')).hexdigest()
    inodeInfo = bucketName + '\n' + relativePath + '\n' + filename + '\n' + str(filesize)
    return fileID, inodeInfo

def saveInode(bucketName, targetFilePath, targetFileRelativePath, savePath):
    filename = os.path.basename(targetFilePath)
    fileinfo = os.stat(targetFilePath) # 获取文件的基本信息
    filesize = fileinfo.st_size

    # 输出文件的基本信息
    # print("文件大小:", fileinfo.st_size)
    # print("索引号:", fileinfo.st_ino)
    # print("设备名:", fileinfo.st_dev)
    # print("最后一次访问时间:", fileinfo.st_atime)
    # print("最后一次修改时间:", fileinfo.st_mtime)
    # print("最后一次状态变化的时间:", fileinfo.st_ctime)

    fileID, inodeInfo = generateInode(bucketName, targetFileRelativePath, filename, filesize)
    inodePath = os.path.join(savePath, fileID)

    f = open(inodePath, 'w', encoding = 'utf8')
    f.write(inodeInfo)
    f.close()
